package com.wizardClient;

import com.appclient.DaoInstanceEnum;
import com.carpark.ejb.DAO.DAO;
import com.carpark.ejb.DAO.DaoImpl;
import com.carpark.ejb.Model.TarrifsModel;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;


/**
 *
 * @author Sawior
 */
public class ChooseTarrifController implements Initializable, ClientControlledScreen {

    private ClientScreenController myController;
    @FXML
    private TextField filterField;
    @FXML
    private TableView<TarrifsModel> tarrifTable;
    @FXML
    private TableColumn<TarrifsModel, String> name;
    @FXML
    private TableColumn<TarrifsModel, String> price;
    @FXML
    private TableColumn<TarrifsModel, String> durationTime;
    @FXML
    private TableColumn<TarrifsModel, String> carType;
    @FXML
    private TableColumn<TarrifsModel, String> startDate;
    @FXML
    private TableColumn<TarrifsModel, String> endDate;
    
    private ObservableList<TarrifsModel> masterData = FXCollections.observableArrayList();
    private ObservableList<TarrifsModel> filteredData = FXCollections.observableArrayList();

    public ChooseTarrifController() {
        List<TarrifsModel> data = DaoInstanceEnum.INSTANCE.getDao().getAllTarrifs();
        for (TarrifsModel tarrifsModel : data) {
            masterData.add(tarrifsModel);
        }

        filteredData.addAll(masterData);
        masterData.addListener(new ListChangeListener<TarrifsModel>() {
            @Override
            public void onChanged(ListChangeListener.Change<? extends TarrifsModel> change) {
                updateFilteredData();
            }
        });
    }

    /**
     * Initializes the controller class. This method is automatically called
     * after the fxml file has been loaded.
     */
    @FXML
    private void initialize() {
        name.setCellValueFactory(
                new PropertyValueFactory<TarrifsModel, String>("name"));
        price.setCellValueFactory(
                new PropertyValueFactory<TarrifsModel, String>("rank"));
        durationTime.setCellValueFactory(
                new PropertyValueFactory<TarrifsModel, String>("durationTime"));
        carType.setCellValueFactory(
                new PropertyValueFactory<TarrifsModel, String>("carType"));
        startDate.setCellValueFactory(
                new PropertyValueFactory<TarrifsModel, String>("issueDate"));
        endDate.setCellValueFactory(
                new PropertyValueFactory<TarrifsModel, String>("endDate"));

        tarrifTable.setItems(filteredData);

        filterField.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable,
                    String oldValue, String newValue) {
                updateFilteredData();
            }
        });
    }

    /**
     * Updates the filteredData to contain all data from the masterData that
     * matches the current filter.
     */
    private void updateFilteredData() {
        filteredData.clear();

        for (TarrifsModel p : masterData) {
            if (matchesFilter(p)) {
                filteredData.add(p);
            }
        }

        // Must re-sort table after items changed
        reapplyTableSortOrder();
    }

    /**
     * Returns true if the person matches the current filter. Lower/Upper case
     * is ignored.
     *
     * @param tarrif
     * @return
     */
    private boolean matchesFilter(TarrifsModel tarrif) {
        String filterString = filterField.getText();
        if (filterString == null || filterString.isEmpty()) {
            return true;
        }
        String lowerCaseFilterString = filterString.toLowerCase();

        if (tarrif.getName().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        } 
        if (tarrif.getCarType().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }
        Double rank = tarrif.getRank();
        if (rank.toString().toLowerCase().indexOf(lowerCaseFilterString) != -1) {
            return true;
        }

        return false;
    }

    private void reapplyTableSortOrder() {
        ArrayList<TableColumn<TarrifsModel, ?>> sortOrder = new ArrayList<>(tarrifTable.getSortOrder());
        tarrifTable.getSortOrder().clear();
        tarrifTable.getSortOrder().addAll(sortOrder);
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        initialize();
    }

    @Override
    public void setScreenParent(ClientScreenController screenParent) {
        myController = screenParent;
    }
    
    public TarrifsModel getTarrifId(){
        return tarrifTable.getSelectionModel().getSelectedItem();
    }

    @Override
    public boolean validateForm() {
        if(tarrifTable.getSelectionModel().getSelectedItem() != null){
            return true;
        }
        return false;
    }
}
